# Maintainer: Jens Staal <staal1978@gmail.com>
# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Natanael Copa <ncopa@alpinelinux.org>
pkgname=wine-staging
pkgver=2.2
_pkgver=${pkgver/_/-}
pkgrel=1
pkgdesc="A compatibility layer for running Windows programs :: Staging"
url="https://wine-staging.com/"
arch="x86_64"
license="LGPL-2.0-or-later"
replaces="wine"
subpackages="$pkgname-libs $pkgname-dev $pkgname-doc"
depends_dev="$pkgname perl"
makedepends="fontconfig-dev openldap-dev libxslt-dev libxxf86dga-dev
	libxcursor-dev libxrandr-dev libxdamage-dev mesa-dev flex-dev bison
	libpng-dev libjpeg-turbo-dev freetype-dev ncurses-dev libressl-dev
	libxcomposite-dev libxrender-dev libxinerama-dev zlib-dev
	cups-dev alsa-lib-dev lcms-dev libxml2-dev paxmark dbus-dev libxi-dev
	gnutls-dev tiff-dev v4l-utils-dev udisks2-dev autoconf automake
	"
source="https://github.com/wine-compholio/wine-patched/archive/staging-$_pkgver.tar.gz
	https://github.com/sarnex/wine-d3d9-patches/archive/wine-d3d9-$_pkgver.tar.gz
	nopie.patch
	winhlp32-flex.patch
	"
builddir="$srcdir/wine-patched-staging-$_pkgver"

prepare() {
	cd "$builddir"

	patch -p1 -i "$srcdir"/wine-d3d9-patches-wine-d3d9-$_pkgver/staging-helper.patch
	patch -p1 -i "$srcdir"/wine-d3d9-patches-wine-d3d9-$_pkgver/wine-d3d9.patch

	default_prepare || return 1
	aclocal && autoheader && autoreconf || return 1
}

build() {
	cd "$builddir"

	case "$CARCH" in
		x86_64) _win64=--enable-win64;;
	esac
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--libdir=/usr/lib \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--with-dbus \
		--with-x \
		--with-d3dadapter \
		--disable-tests \
		$_win64 \
		|| return 1
	make || return 1
}

package() {
	local file
	cd "$builddir"
	make -j1 DESTDIR="$pkgdir" install || return 1

	case "$CARCH" in
		x86_64) _wine="wine64";;
		*) _wine="wine";;
	esac

	paxmark -psmre "$pkgdir"/usr/bin/$_wine-preloader \
		"$pkgdir"/usr/bin/$_wine

	install -Dm755 tools/wineapploader \
		"$pkgdir"/usr/bin/wineapploader || return 1

	for file in msiexec notepad regedit regsvr32 wineboot \
		winecfg wineconsole winefile winemine winepath msidb
	do
		rm "$pkgdir"/usr/bin/$file || return 1
		ln -sf /usr/bin/wineapploader "$pkgdir"/usr/bin/$file
	done
}

dev() {
	local file
	default_dev || return 1
	replaces="wine-dev"

	install -d "$subpkgdir"/usr/bin
	for file in widl wmc wrc winebuild winedump function_grep.pl \
		winedbg winemaker winegcc winecpp wineg++
	do
		mv "$pkgdir"/usr/bin/$file "$subpkgdir"/usr/bin/
	done

	install -d "$subpkgdir"/usr/lib/wine
	mv "$pkgdir"/usr/lib/wine/*.def "$subpkgdir"/usr/lib/wine/
}

doc() {
	default_doc || return 1
	replaces="wine-doc"

	rm -rf "$pkgdir"/usr/share/man
	rm -fr "$subpkgdir"/usr/share/man/*.UTF-8
}

libs() {
	local ext file
	default_libs || return 1
	replaces="wine-libs"

	install -d "$subpkgdir"/usr/lib/wine/fakedlls
	for ext in dll16 drv16 exe16 acm dll drv ds ocx sys tlb vxd; do
		for file in $(find "$pkgdir/usr/lib/wine/fakedlls" -name "*.$ext"); do
			mv "$pkgdir"/usr/lib/wine/fakedlls/${file##*/} \
				"$subpkgdir"/usr/lib/wine/fakedlls/
			mv "$pkgdir"/usr/lib/wine/${file##*/}.so \
				"$subpkgdir"/usr/lib/wine/
		done
	done
}

sha512sums="f1bad84c3e8158d6d43b9a8be59daf02256c4956cc906c85fd8a26b073e768f9f932fb2799e5f5ce41e0614c67df3bdb45bd11ea93f96bccef432a75651c0b74  staging-2.2.tar.gz
6e9e031b51afc8ec200bbd252ff0d768c29af23907f782409834de593f896afb128f86fe012c95dc18e5f3c7267f170eb343d2a2c53d639933a337f0c1e04fab  wine-d3d9-2.2.tar.gz
f4c3ffc86ceb14c865282574f837c2114cdfa38df64327f17c2d5913cb85726d883e4a136ce38297612cda6dd47f9bdf80aff770368b8b846a642337f52a68bf  nopie.patch
522a94a31fc459e80ea7dd05f7aee64f6ae666ec05236d06614acde118d5c60002e0f253ae75edb5f02164f22937ca89578504b690d1a5611bd60f703c8f0c00  winhlp32-flex.patch"
